package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.manager.Datenverwaltung.Albenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Episodenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Musikverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Userverwaltung;
import de.fhb.itunes.repository.DataAccess.DAO_Film;
import de.fhb.itunes.repository.DataAccess.DAO_TvSendung;
import de.fhb.itunes.repository.data.Film_VO;
import de.fhb.itunes.repository.data.User_VO;

/**
 * Servlet implementation class Index
 */
@WebServlet("/Index")
public class Index extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Index() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(false);
		String nextJSP = "/login.jsp";
		if (session != null) {
			Object obj = session.getAttribute("loggedIn");
			if (obj != null) {
				boolean isLoggedIn = (Boolean) obj;
				if (isLoggedIn) {
					nextJSP = "/index.jsp";
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		session.setAttribute("loggedIn", false);
		String nextJSP = "/login.jsp";
		Enumeration en = request.getParameterNames();
		String user = "";
		String pwd = "";
		while (en.hasMoreElements()) {
			String paramName = (String) en.nextElement();
			String paraValue = request.getParameter(paramName);
			if (paramName.contains("user")) {
				user = paraValue;
			} else if (paramName.contains("pwd")) {
				pwd = paraValue;
			}
		}
		if (user != "" && pwd != "") {
			Userverwaltung userverwaltung = new Userverwaltung();
			User_VO tmpuser = userverwaltung.getByName(user);
			if (tmpuser != null && tmpuser.validatePassword(pwd)) {
				session = request.getSession(true);
				session.setAttribute("loggedIn", true);
				session.setAttribute("musik", new Musikverwaltung());
				session.setAttribute("album", Albenverwaltung.getInstance());
				session.setAttribute("tv", new DAO_TvSendung());
				session.setAttribute("episode", new Episodenverwaltung());
				session.setAttribute("film", new DAO_Film());
				session.setAttribute("user", tmpuser);
				Object obj = session.getAttribute("film");
				if (obj instanceof DAO_Film) {
					DAO_Film filme = (DAO_Film) obj;
					List<Film_VO> listofFilms = (List<Film_VO>) filme
							.getLastAdded(5);
					request.setAttribute("lastAdded", listofFilms);

					nextJSP = "/index.jsp";
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);

	}

}
